package com.liu.frame.customview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.liu.frame.R;
import com.liu.frame.utils.ScreenUtils;

/**
 * Company:成都珊瑚虫信息技术有限公司
 * Author:Created by 刘智强 on 2015/10/20.
 * Effect: titleBar
 */

public class MyTitleBar extends FrameLayout {
    public SearchKeyListener searchKeyListener;//搜索键监听器
    private RelativeLayout title_content_layout;
    private LinearLayout title_view;
    private RelativeLayout btn_left;
    private RelativeLayout btn_right;
    private TextView tv_title;
    private TextView tv_right;
    private ImageView iv_right;
    private LinearLayout center_view;
    private TextView btn_brand;  //中左
    private TextView btn_boutique;     //中右
    private LinearLayout center_search; //搜索框
    private ImageView search_iv;//点击搜索
    private EditText et_search;

    public void setSearchKeyListener(SearchKeyListener searchKeyListener) {
        this.searchKeyListener = searchKeyListener;
    }

    public MyTitleBar(Context context) {
        super(context);

        setView(context);
    }

    public MyTitleBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        setView(context);

    }

    public MyTitleBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setView(context);
    }



    private void setView(Context c) {
        LayoutInflater.from(c).inflate(R.layout.title_bar, this);
        title_view = (LinearLayout) findViewById(R.id.title_view);
        btn_left = (RelativeLayout) findViewById(R.id.btn_left);
        btn_right = (RelativeLayout) findViewById(R.id.btn_right);
        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_right = (TextView) findViewById(R.id.tv_right);
        iv_right = (ImageView) findViewById(R.id.iv_right);
        center_view = (LinearLayout) findViewById(R.id.tab_view);
        btn_brand = (TextView) findViewById(R.id.btn_brand);
        btn_boutique = (TextView) findViewById(R.id.btn_boutique);
        center_search = (LinearLayout) findViewById(R.id.center_search);
        et_search = (EditText) findViewById(R.id.search_et);
        search_iv= (ImageView) findViewById(R.id.search_iv);
        title_content_layout= (RelativeLayout) findViewById(R.id.title_content_layout);
        et_search.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH){
                    if(searchKeyListener!=null){
                        searchKeyListener.onSearch();
                    }
                }
                return false;
            }
        });

    }

    /**
     * 设置为透明背景
     * @param c
     */
    public void setTransparentTitleView(Context c,boolean isTran){
        if (isTran){
            title_view.setBackgroundResource(R.drawable.titlebar);
        }else {
            title_view.setBackgroundColor(c.getResources().getColor(R.color._transparent));
        }
    }
    public void setTitleBackground(int color){
        title_view.setBackgroundColor(color);
    }

    /**
     * left
     *
     * @param l
     */
    public void setLeftButtonListener(OnClickListener l) {
        btn_left.setVisibility(VISIBLE);
        btn_left.setOnClickListener(l);
    }


    /**
     * title
     *
     * @param s
     */
    public void setTitle(String s) {
        tv_title.setVisibility(View.VISIBLE);
        tv_title.setText(s);
    }

    /**
     * right tv
     *
     * @param s
     */
    public void setRightTv(String s) {
        tv_right.setText(s);
        tv_right.setVisibility(View.VISIBLE);
    }

    public View getBtnLeftView() {
        btn_left.setVisibility(View.VISIBLE);
        return btn_left;
    }

    public void setRightIv(int id) {
        iv_right.setImageResource(id);
        iv_right.setVisibility(View.VISIBLE);

    }

    /**
     * right
     *
     * @param l
     */
    public void setRightButtonListener(OnClickListener l) {
        btn_right.setVisibility(VISIBLE);
        btn_right.setOnClickListener(l);
    }

    /**
     * tabview
     *
     * @param l1
     * @param l2
     */
    public void setCenterView(OnClickListener l1, OnClickListener l2) {
        center_view.setVisibility(View.VISIBLE);
        btn_boutique.setOnClickListener(l1);
        btn_brand.setOnClickListener(l2);
    }

    /**
     * search
     */
    public void setCenterSearch(String sTrHint) {
        center_search.setVisibility(View.VISIBLE);
        et_search.setHint(sTrHint);
        et_search.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
        et_search.setInputType(EditorInfo.TYPE_CLASS_TEXT);
    }

    /**
     * 点击搜索
     * @param l
     */
    public void setSearchListener(OnClickListener l) {
        search_iv.setOnClickListener(l);
        et_search.setText("");
    }
    public View getEtView() {
        return et_search;
    }

    /**
     * 获取edittext中输入的内容
     *
     * @return
     */
    public String getCenterSearch() {
        String mContent = null;
        mContent = et_search.getText().toString().trim();
        return mContent;
    }

    /**
     * 将设置标题栏距离状态栏高度
     */
    public void setTitleMargnTop(){
        LinearLayout.LayoutParams params= (LinearLayout.LayoutParams) title_content_layout.getLayoutParams();
        params.topMargin= ScreenUtils.getStatusHeight(getContext());
        title_content_layout.setLayoutParams(params);
    }
    public interface SearchKeyListener{
        void onSearch();
    }
}
